package com.zzl.ov_library.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzl.ov_library.dto.Result;
import com.zzl.ov_library.entity.Books;
import com.zzl.ov_library.service.BooksService;
import com.zzl.ov_library.service.BorrowRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class BooksController {

    @Autowired
    private BooksService booksService;

    @Autowired
    private BorrowRecordService borrowRecordService;

    //新增图书信息
    @PostMapping("/books/add")
    public Result addBooks(@RequestBody Books books) {
        //通过ISBN号查询图书信息是否已存在
        Books book = booksService.getOne(Wrappers.lambdaQuery(Books.class).eq(Books::getIsbn, books.getIsbn()));
        if (book != null) {
            return Result.fail().setMsg("图书已存在");
        }

        return booksService.save(books) ? Result.ok().setMsg("添加成功") : Result.fail().setMsg("添加失败");
    }

    //删除图书信息
    @DeleteMapping("/books/delete/{bookId}")
    public Result deleteBooks(@PathVariable Integer bookId) {
        return booksService.removeById(bookId) ? Result.ok().setMsg("删除成功") : Result.fail().setMsg("删除失败");
    }

    //修改图书信息
    @PutMapping("/books/update")
    public Result updateBooks(@RequestBody Books books) {
        return booksService.updateById(books) ? Result.ok().setMsg("修改成功") : Result.fail().setMsg("修改失败");
    }

    //查询图书信息
    @GetMapping("/books/query/{bookId}")
    public Result queryBooks(@PathVariable Integer bookId) {
        Books books = booksService.getById(bookId);
        return books != null ? Result.ok().setData(books) : Result.fail().setMsg("查询失败");
    }

    //通过id列表查询图书信息列表
    @GetMapping("/books/queryByIds")
    public Result queryBooksByIds(@RequestParam List<String> bookIds) {
        List<Books> list = booksService.listByIds(bookIds);
        System.out.println("list:" + list);
        return list.size() > 0 ? Result.ok().setData(list) : Result.fail().setMsg("未查询到数据信息");
    }

    //图书信息列表
    @GetMapping("/books/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit) {
        List<Books> list = booksService.list(new Page<>(page, limit));
        long count = booksService.count();
        System.out.println("list:" + list);
        return Result.ok().setData(list).setCount(count).setMsg("查询成功");

    }

    //根据书名或作者或出版社或ISBN查询图书信息
    @GetMapping("/books/query")
    public Result queryBooks(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "20") Integer limit,
                             @RequestParam Map<String, String> params) {
        String search_type = params.get("search_type");
        String keyword = params.get("keyword");
        List<Books> list = new ArrayList<>();

        switch (search_type) {
            case "bookTitle":
                list = booksService.list(Wrappers.lambdaQuery(Books.class)
                        .like(Books::getBookTitle, keyword));
                break;
            case "author":
                list = booksService.list(Wrappers.lambdaQuery(Books.class)
                        .like(Books::getAuthor, keyword));
                break;
            case "bookPress":
                list = booksService.list(Wrappers.lambdaQuery(Books.class)
                        .like(Books::getBookPress, keyword));
                break;
            case "isbn":
                list = booksService.list(Wrappers.lambdaQuery(Books.class)
                        .like(Books::getIsbn, keyword));
                break;
        }
        System.out.println("list:" + list);
        return list.size() > 0 ? Result.ok().setData(list) : Result.fail().setMsg("未查询到数据信息");
    }

    //热门搜索
    @GetMapping("/books/hotSearch")
    public Result hotSearch() {
        //列表查询
        List<Books> list = booksService.list(Wrappers.lambdaQuery(Books.class)
                .orderByDesc(Books::getCallNumber)
                .last("limit 5"));
        //书籍标题列表
        List<String> bookTitleList = new ArrayList<>();
        for (Books books : list) {
            bookTitleList.add(books.getBookTitle());
        }
        return list.size() > 0 ? Result.ok().setData(bookTitleList) : Result.fail().setMsg("未查询到数据信息");
    }

    //通过分类查询图书信息
    @GetMapping("/books/queryByCategory")
    public Result queryByCategory(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "20") Integer limit,
                                  @RequestParam Map<String, String> params) {
        String category = params.get("category");
        System.out.println("category:" + category);
        //通过category查询图书列表,使用模糊查询
        List<Books> list = booksService.list(Wrappers.lambdaQuery(Books.class)
                .like(Books::getCategory, category)
                .orderByDesc(Books::getCallNumber)
                .last("limit " + page + "," + limit));
        return list.size() > 0 ? Result.ok().setData(list) : Result.fail().setMsg("未查询到数据信息");
    }

    //更新图书索书号
    @PutMapping("/books/updateBookCallNumber")
    public Result updateBookCallNumber() {
        //获取所有图书信息
        List<Books> list = booksService.list();
        String callNumber = "";
        //遍历
        for (int i = 0; i < list.size(); i++) {
            Books books = list.get(i);
            if (books.getCallNumber() == null) {
                //获取图书分类,根据分类生成索书号,分类有'历史', '小说', '文学', '艺术', '哲学', '经济', '心理', '社科'
                String category = books.getCategory();
                switch (category) {
                    case "历史":
                        callNumber = "K";
                        break;
                    case "小说":
                        callNumber = "N";
                        break;
                    case "文学":
                        callNumber = "I";
                        break;
                    case "艺术":
                        callNumber = "J";
                        break;
                    case "哲学":
                        callNumber = "B";
                        break;
                    case "经济":
                        callNumber = "F";
                        break;
                    case "心理":
                        callNumber = "H";
                        break;
                    case "社科":
                        callNumber = "C";
                        break;
                        default:
                        callNumber = "A";
                        break;
                }
                //生成1~999的随机数
                int random = (int) (Math.random() * 999 + 1);
                callNumber = callNumber + random + '-';
                //生成1~99的随机数
                random = (int) (Math.random() * 99 + 1);
                callNumber = callNumber + random + '/';
                //生成1~9999的随机数
                random = (int) (Math.random() * 999 + 1);
                callNumber = callNumber + random;
                System.out.println(callNumber);
                //通过callNumber获取图书信息
                Books books1 = booksService.getOne(Wrappers.lambdaQuery(Books.class)
                        .eq(Books::getCallNumber, callNumber));
                if (books1 != null)
                    i--;
                else
                    booksService.update(Wrappers.lambdaUpdate(Books.class)
                            .set(Books::getCallNumber, callNumber)
                            .eq(Books::getBookId, books.getBookId()));
            }
        }
        return Result.ok();
    }

    //查询热门书籍
    @GetMapping("/books/hotBooks")
    public Result hotBooks() {
        List<Integer> idList = borrowRecordService.listTopBookIds();
        List<Books> list = booksService.listByIds(idList);
        return list.size() > 0 ? Result.ok().setData(list) : Result.fail().setMsg("未查询到数据信息");
    }


}
